/**
 * 
 */
package id.web.faisalabdillah.xtimesheet.web.ws;

import java.util.List;

import id.web.faisalabdillah.xtimesheet.domain.User;
import id.web.faisalabdillah.xtimesheet.services.IUserService;
import id.web.faisalabdillah.xtimesheet.web.dto.DTOJsonResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author faitz
 *
 */
@Controller
@RequestMapping(value="/service/user")
public class UserServiceCtrl {
	
	@Autowired
	private IUserService userService;
	
	@RequestMapping("/get/{id}")
	public @ResponseBody DTOJsonResult<User> getUser(@PathVariable String id){
		DTOJsonResult<User> data=new DTOJsonResult<User>();
		try {
			User user=userService.findById(id);
			data.setData(user);
			data.setSuccess(true);
			data.setResultSize(1);
		} catch (Exception e) {
			e.printStackTrace();
			data.setSuccess(false);
			data.setError(e.getMessage());
		}
		return data;
	}
	
	@RequestMapping("/all")
	public @ResponseBody DTOJsonResult<List<User>> listAll(){
		DTOJsonResult<List<User>> data=new DTOJsonResult<List<User>>();
		try {
			data.setData(userService.listAll());
			data.setSuccess(true);
			data.setResultSize(userService.sizeAll());
		} catch (Exception e) {
			data.setSuccess(false);
			data.setError(e.getMessage());
		}
		return data;
	}
	
	@RequestMapping("/all/{first}/{max}")
	public @ResponseBody DTOJsonResult<List<User>> listAllPaged(@PathVariable int first,@PathVariable int max){
		DTOJsonResult<List<User>> data=new DTOJsonResult<List<User>>();
		try {
			data.setData(userService.listAllPaged(first, max));
			data.setSuccess(true);
			data.setResultSize(userService.sizeAll());
		} catch (Exception e) {
			data.setSuccess(false);
			data.setError(e.getMessage());
		}
		return data;
	}
	
	@RequestMapping("/search")
	public @ResponseBody DTOJsonResult<List<User>> searchByName(@RequestParam(defaultValue="",value="q") String q){
		DTOJsonResult<List<User>> data=new DTOJsonResult<List<User>>();
		try {
			data.setData(userService.searchByName(q));
			data.setSuccess(true);
			data.setResultSize(userService.searchByNameSize(q));
		} catch (Exception e) {
			data.setSuccess(false);
			data.setError(e.getMessage());
		}
		return data;
	}
	@RequestMapping("/search/{first}/{max}")
	public @ResponseBody DTOJsonResult<List<User>> searchByNamePaged(@PathVariable int first,@PathVariable int max,@RequestParam(defaultValue="",value="q") String query){
		DTOJsonResult<List<User>> data=new DTOJsonResult<List<User>>();
		try {
			data.setData(userService.searchByNamePaged(query, first, max));
			data.setSuccess(true);
			data.setResultSize(userService.searchByNameSize(query));
		} catch (Exception e) {
			data.setSuccess(false);
			data.setError(e.getMessage());
		}
		return data;
	}
	
	@RequestMapping("/delete/{id}")
	public @ResponseBody DTOJsonResult<Object> delete(@PathVariable String id){
		DTOJsonResult<Object> data=new DTOJsonResult<Object>();
		try {
			userService.remove(id);
			data.setSuccess(true);
		} catch (Exception e) {
			data.setSuccess(false);
			data.setError(e.getMessage());
		}
		return data;
	}
	
	@RequestMapping(value="/udpate",method=RequestMethod.POST)
	public DTOJsonResult<Object> update(){
		DTOJsonResult<Object> data=new DTOJsonResult<Object>();
		try {
			
		} catch (Exception e) {
			data.setSuccess(false);
			data.setError(e.getMessage());
		}
		return data;
	}
	
	@RequestMapping(value="/udpate",method=RequestMethod.POST)
	public DTOJsonResult<Object> insert(){
		DTOJsonResult<Object> data=new DTOJsonResult<Object>();
		try {
			
		} catch (Exception e) {
			data.setSuccess(false);
			data.setError(e.getMessage());
		}
		return data;
	}
}
